//
//  JkmUIView.swift
//  SinaWb
//
//  Created by Jiang on 16/5/10.
//  Copyright © 2016年 Jiang. All rights reserved.
//

import UIKit
protocol VisitorViewDelegate : NSObjectProtocol{
    
    func ClickLogInButton()
    
    func ClickLogOnButton()
}
class VisitorView: UIView {
    weak var delegate : VisitorViewDelegate?
    init(){
        super.init(frame: CGRectZero)
        setTheUI()
    }
    func setCircleImageAndTextContent(circleimage : UIImage? , text : String){
        TextLable.text = text
        backgroundColor = UIColor(white: 0.93, alpha: 1.0)
        if circleimage == nil {
            StartAnimation()
            return
        }
        backImage.hidden = true
        CircleImageView.image = circleimage
        IconImageView.removeFromSuperview()
    }
    func StartAnimation(){
        let Basic = CABasicAnimation(keyPath: "transform.rotation")
        Basic.duration = 10.0
        Basic.repeatCount = MAXFLOAT
        Basic.byValue = M_PI * 2
        Basic.removedOnCompletion = false
        CircleImageView.layer.addAnimation(Basic, forKey: nil)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setTheUI(){
        addAllViewsAndConstains()
        
        loginButton.addTarget(self, action: #selector(loginButtonClicked), forControlEvents: .TouchUpInside)
        logonButton.addTarget(self, action: #selector(logonButtonClicked), forControlEvents: .TouchUpInside)
    }
    
  @objc private func loginButtonClicked(){
        delegate?.ClickLogInButton()
    }
  @objc private  func logonButtonClicked(){
        delegate?.ClickLogOnButton()
    }
    
    func addAllViewsAndConstains(){
        addSubview(CircleImageView)
        addSubview(backImage)
        addSubview(IconImageView)
        addSubview(TextLable)
        addSubview(logonButton)
        addSubview(loginButton)
        for item  in subviews {
            item.translatesAutoresizingMaskIntoConstraints = false
        }
        
        addConstraint(NSLayoutConstraint(item: CircleImageView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: CircleImageView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: -60))
        
        addConstraint(NSLayoutConstraint(item: IconImageView, attribute: .CenterX, relatedBy: .Equal, toItem: CircleImageView, attribute:.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: IconImageView, attribute: .CenterY, relatedBy: .Equal, toItem: CircleImageView, attribute:.CenterY, multiplier: 1, constant: 0))
        
        let textWidth :CGFloat = 250
        let logbtnWidth :CGFloat = CircleImageView.frame.size.width * 0.45
        
        addConstraint(NSLayoutConstraint(item: TextLable, attribute:.Top, relatedBy: .Equal, toItem: CircleImageView, attribute: .Bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: TextLable, attribute:.CenterX, relatedBy: .Equal, toItem: CircleImageView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: TextLable, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute ,multiplier: 1, constant: textWidth))
        addConstraint(NSLayoutConstraint(item: TextLable, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 60))
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Left, relatedBy: .Equal, toItem: CircleImageView, attribute: .Left, multiplier: 1, constant: 10))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: TextLable, attribute: .Bottom, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: logbtnWidth))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        
        addConstraint(NSLayoutConstraint(item: logonButton, attribute: .Right, relatedBy: .Equal, toItem: CircleImageView, attribute: .Right, multiplier: 1, constant: 10))
        addConstraint(NSLayoutConstraint(item: logonButton, attribute: .Top, relatedBy: .Equal, toItem: TextLable, attribute: .Bottom, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: logonButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: logbtnWidth))
        addConstraint(NSLayoutConstraint(item: logonButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        
        
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backView]-0-|", options: [], metrics: nil, views: ["backView":backImage]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backView]-[logbutton]", options: [], metrics: nil, views: ["backView" : backImage,"logbutton" : logonButton]))

    }
    
    lazy   var CircleImageView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    lazy   var IconImageView : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    lazy   var TextLable : UILabel = {
        let lable = UILabel()
        lable.numberOfLines = 0
        lable.font  = UIFont.systemFontOfSize(14)
        lable.textColor  = UIColor.lightGrayColor()
        lable.textAlignment  = .Center
        lable.text = "上的浪费了多少房间的绿色减肥了"
        return lable
    }()
    
    lazy var loginButton : UIButton  = {
         let logbutton = UIButton(type: UIButtonType.Custom)
        logbutton.setTitle("登录", forState: .Normal)
        logbutton.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        logbutton.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        logbutton.sizeToFit()
        return logbutton
    }()
    
    lazy var logonButton : UIButton = {
        let logbutton = UIButton(type: UIButtonType.Custom)
        logbutton.setTitle("注册", forState: .Normal)
        logbutton.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        logbutton.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        logbutton.sizeToFit()
        return logbutton
    }()
    
    lazy var backImage : UIImageView = {
        
        let back = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return back
    
    }()
}
